package com.sky.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {


    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 添加用户 购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {

        // 1.判断当前 假如购物车的商品是否已经存在
            // 创建shoppingCart 对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
            // 补全用户信息
        Long id = BaseContext.getCurrentId();
        shoppingCart.setUserId(id);
            // 动态查询----由于每次添加的菜品数据，重复的只会number添加一，所有不会有多条数据查出
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        // 2.存在数量加1
        if(ObjectUtil.isNotNull(list)&&list.size()>0){
            // 取出当前查询出来的商品信息
            shoppingCart = list.get(0);
            // 当前商品数量加一
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            // 更新商品数据
            shoppingCartMapper.update(shoppingCart);
        // 3.不存在插入数据
        }else{
            // 补全购物车对象信息    判断是菜品还是套餐
                // 补全菜品信息
            Long dishId = shoppingCartDTO.getDishId();
            if(ObjectUtil.isNotNull(dishId)){
                // 为菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
                // 补全菜单信息
            }else {
                // 为套餐
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);// 第一次添加
            shoppingCart.setCreateTime(LocalDateTime.now());
            if(ObjectUtil.isNotNull(shoppingCart)){
                // 2.插入数据
                shoppingCartMapper.insert(shoppingCart);
            }
        }

    }


    /**
     * 查询用户 购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        // 获取当前 用户id
        Long id = BaseContext.getCurrentId();
        // 构建查询条件
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setId(id);
        // 查询数据库
        List<ShoppingCart>shoppingCartList= shoppingCartMapper.list(shoppingCart);
        return shoppingCartList;
    }


    /**
     * 清空当前用户的购物车
     */
    @Override
    public void cleanShoppingCart() {
        // 获取当前登录用户 id
        Long id = BaseContext.getCurrentId();
        // 清空当前用户购物车
        shoppingCartMapper.delete(id);
    }
}
